 <!-- 登录用户个人信息 -->
 <template>
  <div class="users">
    <el-row>
      <el-col :lg="8" :md="24">
        <el-card class="card">
          <template #header>
            <div class="card-header">
              <span>个人信息</span>
            </div>
          </template>
          <div class="avatar">
            <img :src="userInfo.avatar" alt srcset />
          </div>
          <div class="footer">
            <el-divider></el-divider>

            <li class="item">
              <span>
                <span>
                  <el-icon>
                    <user />
                  </el-icon>
                </span>
                <span>用户名称</span>
              </span>
              <span>{{userInfo.username}}</span>
            </li>

            <el-divider></el-divider>
            <li class="item">
              <span>
                <span>
                  <el-icon>
                    <iphone />
                  </el-icon>
                </span>
                <span>联系方式</span>
              </span>
              <span>{{userInfo.phone}}</span>
            </li>

            <el-divider></el-divider>
            <li class="item">
              <span>
                <span>
                  <el-icon>
                    <coin />
                  </el-icon>
                </span>
                <span>所属角色</span>
              </span>
              <span>{{userInfo.roles[0].name?userInfo.roles[0].name:"暂无角色" }}</span>
            </li>

            <el-divider></el-divider>
            <li class="item">
              <span>
                <span>
                  <el-icon>
                    <calendar />
                  </el-icon>
                </span>
                <span>创建时间</span>
              </span>
              <span>{{userInfo.createdAt }}</span>
            </li>

            <el-divider></el-divider>
          </div>
        </el-card>
      </el-col>
      <el-col :lg="14" :md="24">
        <el-card class="card">
          <template #header>
            <div class="card-header">
              <span>基本资料</span>
            </div>
          </template>
          <el-tabs v-model="activeName" class="demo-tabs" @tab-click="handleClick">
            <el-tab-pane label="基本资料" name="1">
              <!-- 基本资料 -->
              <div class="user-detail">
                <el-form
                  ref="ruleFormRef"
                  :model="ruleForm"
                  status-icon
                  :rules="rules"
                  label-width="120px"
                  class="demo-ruleForm"
                >
                  <el-form-item label="username" prop="username">
                    <el-input v-model="ruleForm.username" type="username" autocomplete="off"></el-input>
                  </el-form-item>

                  <el-form-item label="phone" prop="phone">
                    <el-input v-model.number="ruleForm.phone"></el-input>
                  </el-form-item>
                  <el-form-item>
                    <el-button type="primary" @click="submitForm(ruleFormRef)">确定</el-button>
                    <el-button @click="resetForm(ruleFormRef)">修改</el-button>
                  </el-form-item>
                </el-form>
              </div>
            </el-tab-pane>
            <!-- 修改密码 -->
            <el-tab-pane label="修改密码" name="2">
              <el-form
                ref="ruleFormPwdRef"
                :model="ruleFormPwd"
                status-icon
                :rules="rulespwd"
                label-width="120px"
                class="demo-ruleForm"
              >
                <el-form-item label="新密码" prop="password">
                  <el-input v-model="ruleFormPwd.password" type="password" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="确认密码" prop="checkPass">
                  <el-input v-model="ruleFormPwd.checkPass" type="password" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item>
                  <el-button type="primary" @click="submitForm(ruleFormPwdRef)">提交</el-button>
                </el-form-item>
              </el-form>
            </el-tab-pane>
          </el-tabs>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>
 
 <script setup>
import { ref, reactive } from 'vue'
import { UpdateUser, updatePwd } from '@/api/user.js'
import { ElMessage } from 'element-plus'
import store from '@/store'
const userInfo = ref({})
userInfo.value = store.getters.userInfo
const ruleFormRef = ref()
const ruleFormPwdRef = ref()
const disabled = ref(true)

const activeName = ref('1')

const rules = reactive({
  username: [
    {
      required: true,
      message: '请输入用户名称',
      trigger: 'blur',
    },
    {
      max: 20,
      message: '最大20 个字符',
      trigger: 'blur',
    },
  ],
  phone: [
    {
      message: '请输入用户标识',
      trigger: 'blur',
    },
    {
      min: 11,
      max: 11,
      message: '请输入正确格式',
      trigger: 'blur',
    },
  ],
})

const ruleForm = reactive({
  id: userInfo.value.id,
  username: userInfo.value.username,
  phone: userInfo.value.phone,
})
const ruleFormPwd = reactive({
  id: userInfo.value.id,
  password: '',
  checkPass: '',
})

const submitForm = (FormInstance) => {
  FormInstance.validate(async (valid) => {
    if (!valid) return
    let data
    if (activeName.value === '1') {
      data = await UpdateUser(ruleForm)
    } else {
      let res = await updatePwd({
        id: ruleFormPwd.id,
        password: ruleFormPwd.password,
      })
      data = res
    }
    if (data.code === 200) {
      ruleFormPwd.password = ''
      ruleFormPwd.checkPass = ''
      ElMessage.success(data.msg)
    }
  })
}
const handleClick = (tab, event) => {
  // console.log(tab, event)
}

const resetForm = (FormInstance) => {
  FormInstance.resetFields()
}
const validatePass = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请输入密码'))
  } else {
    if (ruleFormPwd.checkPass !== '') {
      if (!ruleFormPwdRef.value) return
      ruleFormPwdRef.value.validateField('checkPass', () => null)
    }
    callback()
  }
}
const validatePass2 = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请再次输入密码'))
  } else if (value !== ruleFormPwd.password) {
    callback(new Error('两次密码不一致!'))
  } else {
    callback()
  }
}
const rulespwd = reactive({
  password: [{ validator: validatePass, trigger: 'blur' }],
  checkPass: [{ validator: validatePass2, trigger: 'blur' }],
})
</script>
 <style lang='less' scoped>
.users {
  margin: 15px;
  box-shadow: none;
}
::v-deep .el-card__header {
  padding: 10px 20px;
}
.avatar {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  margin: auto;
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
  }
}
li {
  list-style: none;
  span {
    font-size: 14px;
  }
}
.item {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.el-divider--horizontal {
  margin: 12px 0;
}
.el-icon {
  vertical-align: middle;
  margin-right: 2px;
}
.card {
  height: 400px;
  margin: 15px;
}
</style>